<template>
	<el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
			label-width="80px">
			<el-form-item label="权限名称" prop="permission">
				<el-input v-model="dataForm.permission" placeholder="权限名称"></el-input>
			</el-form-item>
			<el-form-item label="描述" prop="desc">
				<el-input v-model="dataForm.desc" placeholder="描述"></el-input>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
	export default {
		data() {
			return {
				visible: false,
				dataForm: {
					id: 0,
					permission: '',
					desc: '',
					createTime: '',
					modifyTime: ''
				},
				dataRule: {
					permission: [{
						required: true,
						message: '权限名称不能为空',
						trigger: 'blur'
					}],
					desc: [{
						required: true,
						message: '描述不能为空',
						trigger: 'blur'
					}]
				}
			}
		},
		methods: {
			init(id) {
				this.dataForm.id = id || 0
				this.visible = true
				this.$nextTick(() => {
					this.$refs['dataForm'].resetFields()
					if (this.dataForm.id) {
						this.$http.post('/permission/searchPermissionInfo', {
							id: this.dataForm.id
						}).then(resp => {
							if (resp.data.code === 200) {
								this.dataForm.permission = resp.data.permission
								this.dataForm.desc = resp.data.desc
							}
						})
					}
				})
			},
			// 表单提交
			dataFormSubmit() {
				this.$refs['dataForm'].validate((valid) => {
					if (valid) {
						this.$http.post(`/permission/${this.dataForm.id ? 'update' :'insert'}`, {
							id: this.dataForm.id == null ? undefined : this.dataForm.id,
							permission: this.dataForm.permission,
							desc: this.dataForm.desc
						}).then(resp => {
							if (resp.data.code === 200) {
								this.$message({
									message: '操作成功',
									type: 'success',
									duration: 1200
								})
								this.visible = false;
								this.$emit('refreshDataList')
							} else {
								this.$message({
									message: '操作失败',
									type: 'error',
									duration: 1200
								})
								this.visible = false
							}
						})
					}
				})
			}
		}
	}
</script>